import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import pickle
plt.style.use("ggplot")
plt.rcParams['font.family'] = 'Noto Sans CJK TC'
plt.rcParams['axes.unicode_minus']=False
#custom_params = {"axes.spines.right": False, "axes.spines.top": False}
#sns.set_theme(font='Noto Sans CJK TC', palette=sns.color_palette(), style="ticks", rc=custom_params)
# load 從資料檔案中讀取資料,並轉換為python的資料結構
with open("C:/Users/Rou_yi/OneDrive/上課/統計諮詢/統諮期末報告/data/stop_weather_data.pkl", 'rb+') as f:
weather_data = pickle.load(f)
weather_data = pd.DataFrame(weather_data)
weather_data = weather_data.replace({"X":0, "...":np.nan, "&":0})
for col in ['year', 'month', 'day', 'temp', 'RH', 'WS', 'Precp']:
weather_data[col] = pd.to_numeric(weather_data[col])
weather_data.columns
weather_data.head()
weather_data_2 = weather_data[['year', 'month', '觀測站點', 'temp', 'RH', 'WS', 'Precp']].groupby(by=['year', 'month']).mean()
weather_data_2 = weather_data_2.reset_index()
weather_data_2.head()
data = pd.read_excel("C:/Users/Rou_yi/OneDrive/上課/統計諮詢/統諮期末報告/data/各站每月使用次數.xlsx")
data['total'] = data[['1日', '2日', '3日', '4日', '5日', '6日', '7日', '8日', '9日',
'10日', '11日', '12日', '13日', '14日', '15日', '16日', '17日', '18日', '19日',
'20日', '21日', '22日', '23日', '24日', '25日', '26日', '27日', '28日', '29日',
'30日', '31日']].sum(axis=1)
data.head()
data.columns
data_2 = data.drop(columns=['編號']).groupby(by=['年', '月']).mean()
data_2 = data_2.reset_index()
data_2.head()
color = [sns.color_palette("tab10")[1],
sns.color_palette("tab10")[9],
sns.color_palette("tab10")[2],
sns.color_palette("tab10")[0]]
custom_params = {"axes.spines.top": False}
sns.set_theme(font='Noto Sans CJK TC', palette=sns.color_palette(), style="ticks", rc=custom_params)
df_1 = weather_data_2[weather_data_2['year']==2021]
df_2 = data_2[data_2['年']==2021]
name = ['溫度', '濕度', '風速', '降水量']
fig, axes = plt.subplots(2, 2, figsize=(15, 8))
for i, item in enumerate(['temp', 'RH', 'WS', 'Precp']):
ax1 = axes[i//2][i%2]
color_1 = 'black'
sns.lineplot(data=df_2, x='月', y='total', color=color_1, ax=ax1)
ax1.set_ylabel('使用量', color = color_1)
ax1.tick_params(axis ='y', labelcolor = color_1)
ax2 = ax1.twinx()
ax2.set_ylabel(name[i], color = color[i])
ax2.tick_params(axis ='y', labelcolor = color[i])
sns.lineplot(data=df_1, x='month', y=item, color=color[i], ax=ax2)
plt.title('2021年使用量與'+name[i]+'關係圖')
fig.tight_layout()
# 繪圖設置
custom_params = {"axes.spines.top": False}
sns.set_theme(font='Noto Sans CJK TC', palette=sns.color_palette(), style="ticks", rc=custom_params)
# 多子圖設置
fig, axes = plt.subplots(20, 4, figsize=(24, 80))
st_list = data['站場'].unique()
for i, st in enumerate(st_list):
# 定位第幾子圖
ax1 = axes[i//4][i%4]
# 擷取需要資料
df_1 = weather_data[weather_data['StationName']==st].drop(columns=['StationName', 'Address', '觀測站點', 'day'])
df_1 = df_1.groupby(by=['year', 'month']).mean().reset_index()
df_1 = df_1[df_1['year']==2021]
df_2 = data[(data['站場']==st) & (data['年']==2021)]
# 開始繪圖
color_1 = sns.color_palette("tab10")[1]
sns.lineplot(data=df_1, x='month', y='temp', color=color_1, ax=ax1)
ax1.set_ylabel('溫度', color = color_1)
ax1.tick_params(axis ='y', labelcolor = color_1)
ax2 = ax1.twinx()
color_2 = 'black'
sns.lineplot(data=df_2, x='月', y='total', color = color_2, ax=ax2)
ax2.set_ylabel('使用量', color = color_2)
ax2.tick_params(axis ='y', labelcolor = color_2)
plt.title('{}. 2021年 - {}'.format(i+1, st))
fig.tight_layout()
# 繪圖設置
custom_params = {"axes.spines.top": False}
sns.set_theme(font='Noto Sans CJK TC', palette=sns.color_palette(), style="ticks", rc=custom_params)
# 多子圖設置
fig, axes = plt.subplots(20, 4, figsize=(24, 80))
st_list = data['站場'].unique()
for i, st in enumerate(st_list):
# 定位第幾子圖
ax1 = axes[i//4][i%4]
# 擷取需要資料
df_1 = weather_data[weather_data['StationName']==st].drop(columns=['StationName', 'Address', '觀測站點', 'day'])
df_1 = df_1.groupby(by=['year', 'month']).mean().reset_index()
df_1 = df_1[df_1['year']==2021]
df_2 = data[(data['站場']==st) & (data['年']==2021)]
# 開始繪圖
color_1 = sns.color_palette("tab10")[9]
sns.lineplot(data=df_1, x='month', y='RH', color=color_1, ax=ax1)
ax1.set_ylabel('濕度', color = color_1)
ax1.tick_params(axis ='y', labelcolor = color_1)
ax2 = ax1.twinx()
color_2 = 'black'
sns.lineplot(data=df_2, x='月', y='total', color = color_2, ax=ax2)
ax2.set_ylabel('使用量', color = color_2)
ax2.tick_params(axis ='y', labelcolor = color_2)
plt.title('{}. 2021年 - {}'.format(i+1, st))
fig.tight_layout()
# 繪圖設置
custom_params = {"axes.spines.top": False}
sns.set_theme(font='Noto Sans CJK TC', palette=sns.color_palette(), style="ticks", rc=custom_params)
# 多子圖設置
fig, axes = plt.subplots(20, 4, figsize=(24, 80))
st_list = data['站場'].unique()
for i, st in enumerate(st_list):
# 定位第幾子圖
ax1 = axes[i//4][i%4]
# 擷取需要資料
df_1 = weather_data[weather_data['StationName']==st].drop(columns=['StationName', 'Address', '觀測站點', 'day'])
df_1 = df_1.groupby(by=['year', 'month']).mean().reset_index()
df_1 = df_1[df_1['year']==2021]
df_2 = data[(data['站場']==st) & (data['年']==2021)]
# 開始繪圖
color_1 = sns.color_palette("tab10")[2]
sns.lineplot(data=df_1, x='month', y='WS', color=color_1, ax=ax1)
ax1.set_ylabel('風速', color = color_1)
ax1.tick_params(axis ='y', labelcolor = color_1)
ax2 = ax1.twinx()
color_2 = 'black'
sns.lineplot(data=df_2, x='月', y='total', color = color_2, ax=ax2)
ax2.set_ylabel('使用量', color = color_2)
ax2.tick_params(axis ='y', labelcolor = color_2)
plt.title('{}. 2021年 - {}'.format(i+1, st))
fig.tight_layout()
# 繪圖設置
custom_params = {"axes.spines.top": False}
sns.set_theme(font='Noto Sans CJK TC', palette=sns.color_palette(), style="ticks", rc=custom_params)
# 多子圖設置
fig, axes = plt.subplots(20, 4, figsize=(24, 80))
st_list = data['站場'].unique()
for i, st in enumerate(st_list):
# 定位第幾子圖
ax1 = axes[i//4][i%4]
# 擷取需要資料
df_1 = weather_data[weather_data['StationName']==st].drop(columns=['StationName', 'Address', '觀測站點', 'day'])
df_1 = df_1.groupby(by=['year', 'month']).mean().reset_index()
df_1 = df_1[df_1['year']==2021]
df_2 = data[(data['站場']==st) & (data['年']==2021)]
# 開始繪圖
color_1 = sns.color_palette("tab10")[0]
sns.lineplot(data=df_1, x='month', y='Precp', color=color_1, ax=ax1)
ax1.set_ylabel('降水量', color = color_1)
ax1.tick_params(axis ='y', labelcolor = color_1)
ax2 = ax1.twinx()
color_2 = 'black'
sns.lineplot(data=df_2, x='月', y='total', color = color_2, ax=ax2)
ax2.set_ylabel('使用量', color = color_2)
ax2.tick_params(axis ='y', labelcolor = color_2)
plt.title('{}. 2021年 - {}'.format(i+1, st))
fig.tight_layout()
以月分的角度觀看各站點的使用量,可以發現今年 6 月台南下了很多雨,影響到該月的使用量為大多數站點的使用量。
以下將 6 月的 30 天使用量和降雨量進行繪圖分析,可以看到確實在大多數有下雨的日子,使用量通常會極低,但有某些站點較不呈現此關係,如:
而以上地點大多位於「學校」、「火車站」附近。
day_col = ['1日', '2日', '3日', '4日', '5日', '6日', '7日', '8日', '9日', '10日',
'11日', '12日', '13日', '14日', '15日', '16日', '17日', '18日', '19日', '20日',
'21日', '22日', '23日', '24日', '25日', '26日', '27日', '28日', '29日', '30日', '31日']
# 繪圖設置
custom_params = {"axes.spines.top": False}
sns.set_theme(font='Noto Sans CJK TC', palette=sns.color_palette(), style="ticks", rc=custom_params)
# 多子圖設置
fig, axes = plt.subplots(40, 2, figsize=(20, 160))
st_list = data['站場'].unique()
for i, st in enumerate(st_list):
# 定位第幾子圖
ax1 = axes[i//2][i%2]
# 擷取需要資料
df_1 = weather_data[(weather_data['year']==2021) & (weather_data['month']==6) & (weather_data['StationName']==st)]
df_2 = data[(data['年']==2021) & (data['月']==6) & (data['站場']==st)][day_col].transpose().reset_index()
if df_2.shape[1] < 2:
df_2['value'] = [0]*df_2.shape[0]
df_2.columns = ['day', 'value']
df_2['day'] = list(range(1, df_2.shape[0]+1))
# 開始繪圖
color_1 = sns.color_palette("tab10")[0]
sns.lineplot(data=df_1, x='day', y='Precp', color=color_1, ax=ax1)
ax1.set_ylabel('降水量', color = color_1)
ax1.tick_params(axis ='y', labelcolor = color_1)
ax2 = ax1.twinx()
color_2 = 'black'
sns.lineplot(data=df_2, x='day', y='value', color = color_2, ax=ax2)
ax2.set_ylabel('使用量', color = color_2)
ax2.tick_params(axis ='y', labelcolor = color_2)
plt.title('{}. 2021年6月 - {}'.format(i+1, st))
fig.tight_layout()
給大家愛心 🧡💛💚💙💜